package com.mdd.admin.service.impl.yylq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.yylq.ITAppEnterpriseQualificationRecordService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.yylq.TAppEnterpriseQualificationRecordCreateValidate;
import com.mdd.admin.validate.yylq.TAppEnterpriseQualificationRecordSearchValidate;
import com.mdd.admin.validate.yylq.TAppEnterpriseQualificationRecordUpdateValidate;
import com.mdd.admin.vo.yylq.TAppEnterpriseQualificationRecordDetailVo;
import com.mdd.admin.vo.yylq.TAppEnterpriseQualificationRecordListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppEnterpriseQualificationRecord;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppEnterpriseQualificationRecordMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 企业资质申请记录实现类
 * @author LikeAdmin
 */
@Service
public class TAppEnterpriseQualificationRecordServiceImpl implements ITAppEnterpriseQualificationRecordService {
        
    @Resource
    TAppEnterpriseQualificationRecordMapper tAppEnterpriseQualificationRecordMapper;

    /**
     * 企业资质申请记录列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TAppEnterpriseQualificationRecordListedVo>
     */
    @Override
    public PageResult<TAppEnterpriseQualificationRecordListedVo> list(PageValidate pageValidate, TAppEnterpriseQualificationRecordSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TAppEnterpriseQualificationRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tAppEnterpriseQualificationRecordMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:userId@user_id:long",
            "=:enterpriseId@enterprise_id:int",
            "=:status:int",
            "=:reason:str",
            "like:userName@user_name:str",
            "like:enterpriseName@enterprise_name:str",
        });

        IPage<TAppEnterpriseQualificationRecord> iPage = tAppEnterpriseQualificationRecordMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TAppEnterpriseQualificationRecordListedVo> list = new LinkedList<>();
        for(TAppEnterpriseQualificationRecord item : iPage.getRecords()) {
            TAppEnterpriseQualificationRecordListedVo vo = new TAppEnterpriseQualificationRecordListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime().getTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime().getTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 企业资质申请记录详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TAppEnterpriseQualificationRecord
     */
    @Override
    public TAppEnterpriseQualificationRecordDetailVo detail(Integer id) {
        TAppEnterpriseQualificationRecord model = tAppEnterpriseQualificationRecordMapper.selectOne(
                new QueryWrapper<TAppEnterpriseQualificationRecord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TAppEnterpriseQualificationRecordDetailVo vo = new TAppEnterpriseQualificationRecordDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 企业资质申请记录新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TAppEnterpriseQualificationRecordCreateValidate createValidate) {
        TAppEnterpriseQualificationRecord model = new TAppEnterpriseQualificationRecord();
        model.setUserId(createValidate.getUserId());
        model.setEnterpriseId(createValidate.getEnterpriseId());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        model.setStatus(createValidate.getStatus());
        model.setReason(createValidate.getReason());
        model.setUserName(createValidate.getUserName());
        model.setEnterpriseName(createValidate.getEnterpriseName());
        tAppEnterpriseQualificationRecordMapper.insert(model);
    }

    /**
     * 企业资质申请记录编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TAppEnterpriseQualificationRecordUpdateValidate updateValidate) {
        TAppEnterpriseQualificationRecord model = tAppEnterpriseQualificationRecordMapper.selectOne(
                new QueryWrapper<TAppEnterpriseQualificationRecord>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUserId(updateValidate.getUserId());
        model.setEnterpriseId(updateValidate.getEnterpriseId());
        model.setUpdateTime(new Date());
        model.setStatus(updateValidate.getStatus());
        model.setReason(updateValidate.getReason());
        model.setUserName(updateValidate.getUserName());
        model.setEnterpriseName(updateValidate.getEnterpriseName());
        tAppEnterpriseQualificationRecordMapper.updateById(model);
    }

    /**
     * 企业资质申请记录删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TAppEnterpriseQualificationRecord model = tAppEnterpriseQualificationRecordMapper.selectOne(
                new QueryWrapper<TAppEnterpriseQualificationRecord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tAppEnterpriseQualificationRecordMapper.delete(new QueryWrapper<TAppEnterpriseQualificationRecord>().eq("id", id));
    }

}
